use axum::{extract::{FromRequestParts, Request, State}, http::StatusCode, middleware::Next, response::{IntoResponse, Response}};
use axum_extra::{headers::{authorization::Bearer, Authorization}, TypedHeader};
use tracing::warn;

use super::TokenVerify;



#[allow(unused)]
pub async fn verify_token<T>(State(state): State<T>,req: Request,next: Next) -> Response 
where T: TokenVerify + Clone + Send + Sync + 'static, {
    let (mut parts, body) = req.into_parts();
    let req = match TypedHeader::<Authorization<Bearer>>::from_request_parts(&mut parts, &state).await{
        Ok(TypedHeader(Authorization(bearer))) =>{
            let token = bearer.token();
            match state.verify(token){
                Ok(user) => {
                    let mut req = Request::from_parts(parts, body);
                    req.extensions_mut().insert(user);
                    req
                }
                Err(e) => {
                    let msg = format!("Failed to verify token: {:?}", e);
                    warn!("{}", msg);
                    return (StatusCode::FORBIDDEN, msg).into_response();
                }
            }
        },
        Err(e) => {
            let msg = format!("Failed to parse authorization header: {}", e);
            warn!("{}", msg);
            return (StatusCode::UNAUTHORIZED, msg).into_response();
        }
    };
    next.run(req).await
}